home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 326-350 / disk_337 / cmanual / gadgets.lzh / Gadgets / Example4.c < prev    next >
C/C++ Source or Header  |  1990-01-30  |  12KB  |  318 lines

  1. /* Example4                                                           */
  2. /* This program will open a normal window which is connected to the   */
  3. /* Workbench Screen. The window will use all System Gadgets, and will */
  4. /* close first when the user has selected the System gadget Close     */
  5. /* window. Inside the window we have put two Boolean gadgets with the */
  6. /* text "GADGET 1" and "GADGET 2".                                    */
  7.  
  8.  
  9.  
  10. #include <intuition/intuition.h>
  11.  
  12.  
  13.  
  14. struct IntuitionBase *IntuitionBase;
  15.  
  16.  
  17.  
  18. /* The coordinates for the box: */
  19. SHORT my_points[]=
  20. {
  21.    0,  0, /* Start at position (0,0) */
  22.   70,  0, /* Draw a line to the right to position (70,0) */
  23.   70, 10, /* Draw a line down to position (70,10) */
  24.    0, 10, /* Draw a line to the right to position (0,10) */
  25.    0,  0  /* Finish of by drawing a line up to position (0,0) */ 
  26. };
  27.  
  28. /* The Border structure: */
  29. struct Border my_border=
  30. {
  31.   0, 0,        /* LeftEdge, TopEdge. */
  32.   1,           /* FrontPen, colour register 1. */
  33.   0,           /* BackPen, for the moment unused. */
  34.   JAM1,        /* DrawMode, draw the lines with colour 1. */
  35.   5,           /* Count, 5 pair of coordinates in the array. */
  36.   my_points,   /* XY, pointer to the array with the coordinates. */
  37.   NULL,        /* NextBorder, no other Border structures are connected. */
  38. };
  39.  
  40. /* We can use the same Border structure for both of the gadgets since */
  41. /* they have the same size. */ 
  42.  
  43.  
  44.  
  45. /************/
  46. /* GADGET 1 */
  47. /************/
  48.  
  49. /* The first text string: */
  50. UBYTE my_first_string[]="GADGET 1";
  51.  
  52. /* The IntuiText structure: */
  53. struct IntuiText my_first_text=
  54. {
  55.   1,         /* FrontPen, colour register 1. */
  56.   0,         /* BackPen, colour register 0. */
  57.   JAM1,      /* DrawMode, draw the characters with colour 1, do not */
  58.              /* change the background. */ 
  59.   4, 2,      /* LeftEdge, TopEdge. */
  60.   NULL,      /* ITextFont, use default font. */
  61.   my_first_string, /* IText, the text that will be printed. */
  62.              /* (Remember my_text = &my_text[0].) */
  63.   NULL,      /* NextText, no other IntuiText structures are connected. */
  64. };
  65.  
  66. struct Gadget my_first_gadget=
  67. {
  68.   NULL,          /* NextGadget, no more gadgets in the list. */
  69.   40,            /* LeftEdge, 40 pixels out. */
  70.   20,            /* TopEdge, 20 lines down. */
  71.   71,            /* Width, 71 pixels wide. */
  72.   11,            /* Height, 11 pixels lines heigh. */
  73.   GADGHCOMP,     /* Flags, when this gadget is highlighted, the gadget */
  74.                  /* will be rendered in the complement colours. */
  75.                  /* (Colour 0 (00) will be changed to colour 3 (11) */
  76.                  /* (Colour 1 (01)           - " -           2 (10) */
  77.                  /* (Colour 2 (10)           - " -           1 (01) */
  78.                  /* (Colour 3 (11)           - " -           0 (00) */  
  79.   GADGIMMEDIATE| /* Activation, our program will recieve a message when */
  80.   RELVERIFY,     /* the user has selected this gadget, and when the user */
  81.                  /* has released it. */ 
  82.   BOOLGADGET,    /* GadgetType, a Boolean gadget. */
  83.   (APTR) &my_border, /* GadgetRender, a pointer to our Border structure. */
  84.                  /* (Since Intuition does not know if this will be a */
  85.                  /* pointer to a Border structure or an Image structure, */
  86.                  /* Intuition expects an APTR (normal memory pointer). */
  87.                  /* We will therefore have to calm down the compiler by */
  88.                  /* doing some "casting".) */
  89.   NULL,          /* SelectRender, NULL since we do not supply the gadget */
  90.                  /* with an alternative image. (We complement the */
  91.                  /* colours instead) */
  92.   &my_first_text,/* GadgetText, a pointer to our IntuiText structure. */
  93.                  /* (See chapter 3 GRAPHICS for more information) */
  94.   NULL,          /* MutualExclude, no mutual exclude. */
  95.   NULL,          /* SpecialInfo, NULL since this is a Boolean gadget. */
  96.                  /* (It is not a Proportional/String or Integer gdget) */
  97.   0,             /* GadgetID, no id. */
  98.   NULL           /* UserData, no user data connected to the gadget. */
  99. };
  100.  
  101.  
  102.  
  103. /************/
  104. /* GADGET 2 */
  105. /************/
  106.  
  107. /* The second text string: */
  108. UBYTE my_second_string[]="GADGET 2";
  109.  
  110. /* The IntuiText structure: */
  111. struct IntuiText my_second_text=
  112. {
  113.   1,         /* FrontPen, colour register 1. */
  114.   0,         /* BackPen, colour register 0. */
  115.   JAM1,      /* DrawMode, draw the characters with colour 1, do not */
  116.              /* change the background. */ 
  117.   4, 2,      /* LeftEdge, TopEdge. */
  118.   NULL,      /* ITextFont, use default font. */
  119.   my_second_string, /* IText, the text that will be printed. */
  120.              /* (Remember my_text = &my_text[0].) */
  121.   NULL,      /* NextText, no other IntuiText structures are connected. */
  122. };
  123.  
  124. struct Gadget my_second_gadget=
  125. {
  126.   &my_first_gadget, /* NextGadget, after this comes my_first_gadget. */
  127.   150,           /* LeftEdge, 150 pixels out. */
  128.   20,            /* TopEdge, 20 lines down. */
  129.   71,            /* Width, 71 pixels wide. */
  130.   11,            /* Height, 11 pixels lines heigh. */
  131.   GADGHCOMP,     /* Flags, when this gadget is highlighted, the gadget */
  132.                  /* will be rendered in the complement colours. */
  133.                  /* (Colour 0 (00) will be changed to colour 3 (11) */
  134.                  /* (Colour 1 (01)           - " -           2 (10) */
  135.                  /* (Colour 2 (10)           - " -           1 (01) */
  136.                  /* (Colour 3 (11)           - " -           0 (00) */  
  137.   GADGIMMEDIATE| /* Activation, our program will recieve a message when */
  138.   RELVERIFY,     /* the user has selected this gadget, and when the user */
  139.                  /* has released it. */ 
  140.   BOOLGADGET,    /* GadgetType, a Boolean gadget. */
  141.   (APTR) &my_border, /* GadgetRender, a pointer to our Border structure. */
  142.                  /* (Since Intuition does not know if this will be a */
  143.                  /* pointer to a Border structure or an Image structure, */
  144.                  /* Intuition expects an APTR (normal memory pointer). */
  145.                  /* We will therefore have to calm down the compiler by */
  146.                  /* doing some "casting".) */
  147.   NULL,          /* SelectRender, NULL since we do not supply the gadget */
  148.                  /* with an alternative image. (We complement the */
  149.                  /* colours instead) */
  150.   &my_second_text,/* GadgetText, a pointer to our IntuiText structure. */
  151.                  /* (See chapter 3 GRAPHICS for more information) */
  152.   NULL,          /* MutualExclude, no mutual exclude. */
  153.   NULL,          /* SpecialInfo, NULL since this is a Boolean gadget. */
  154.                  /* (It is not a Proportional/String or Integer gdget) */
  155.   0,             /* GadgetID, no id. */
  156.   NULL           /* UserData, no user data connected to the gadget. */
  157. };
  158.  
  159.  
  160.  
  161. /* Declare a pointer to a Window structure: */ 
  162. struct Window *my_window;
  163.  
  164. /* Declare and initialize your NewWindow structure: */
  165. struct NewWindow my_new_window=
  166. {
  167.   50,            /* LeftEdge    x position of the window. */
  168.   25,            /* TopEdge     y positio of the window. */
  169.   320,           /* Width       320 pixels wide. */
  170.   100,           /* Height      100 lines high. */
  171.   0,             /* DetailPen   Text should be drawn with colour reg. 0 */
  172.   1,             /* BlockPen    Blocks should be drawn with colour reg. 1 */
  173.   CLOSEWINDOW|   /* IDCMPFlags  The window will give us a message if the */
  174.                  /*             user has selected the Close window gad, */
  175.   GADGETDOWN|    /*             or a gadget has been pressed on, or */
  176.   GADGETUP,      /*             a gadge has been released. */
  177.   SMART_REFRESH| /* Flags       Intuition should refresh the window. */
  178.   WINDOWCLOSE|   /*             Close Gadget. */
  179.   WINDOWDRAG|    /*             Drag gadget. */
  180.   WINDOWDEPTH|   /*             Depth arrange Gadgets. */
  181.   WINDOWSIZING|  /*             Sizing Gadget. */
  182.   ACTIVATE,      /*             The window should be Active when opened. */
  183.   &my_second_gadget, /* FirstGadget A pointer to my_second_gadget
  184.                      /* structure. */
  185.   NULL,          /* CheckMark   Use Intuition's default CheckMark. */
  186.   "TOUCH ME",    /* Title       Title of the window. */
  187.   NULL,          /* Screen      Connected to the Workbench Screen. */
  188.   NULL,          /* BitMap      No Custom BitMap. */
  189.   320,           /* MinWidth    We will not allow the window to become */
  190.   50,            /* MinHeight   smaller than 320 x 50, and not bigger */
  191.   640,           /* MaxWidth    than 640 x 200. */
  192.   200,           /* MaxHeight */
  193.   WBENCHSCREEN   /* Type        Connected to the Workbench Screen. */
  194. };
  195.  
  196.  
  197.  
  198. main()
  199. {
  200.   /* Boolean variable used for the while loop: */
  201.   BOOL close_me;
  202.  
  203.  
  204.   /* Declare a variable in which we will store the IDCMP flag: */
  205.   ULONG class;
  206.  
  207.   /* In this example we also need to store the address of the gadget */
  208.   /* which sent us the mesage: */
  209.   APTR address;
  210.   
  211.   /* we declare a memory pointer (APTR) called address. */
  212.  
  213.    
  214.   /* Declare a pointer to an IntuiMessage structure: */
  215.   struct IntuiMessage *my_message;
  216.  
  217.  
  218.  
  219.   /* Before we can use Intuition we need to open the Intuition Library: */
  220.   IntuitionBase = (struct IntuitionBase *)
  221.     OpenLibrary( "intuition.library", 0 );
  222.   
  223.   if( IntuitionBase == NULL )
  224.     exit(); /* Could NOT open the Intuition Library! */
  225.  
  226.  
  227.  
  228.   /* We will now try to open the window: */
  229.   my_window = (struct Window *) OpenWindow( &my_new_window );
  230.   
  231.   /* Have we opened the window succesfully? */
  232.   if(my_window == NULL)
  233.   {
  234.     /* Could NOT open the Window! */
  235.     
  236.     /* Close the Intuition Library since we have opened it: */
  237.     CloseLibrary( IntuitionBase );
  238.  
  239.     exit();  
  240.   }
  241.  
  242.  
  243.  
  244.   /* We have opened the window, and everything seems to be OK. */
  245.  
  246.  
  247.  
  248.   close_me = FALSE;
  249.  
  250.   /* Stay in the while loop until the user has selected the Close window */
  251.   /* gadget: */
  252.   while( close_me == FALSE )
  253.   {
  254.     /* Wait until we have recieved a message: */
  255.     Wait( 1 << my_window->UserPort->mp_SigBit );
  256.  
  257.     /* Collect the message: */
  258.     my_message = (struct IntuiMessage *) GetMsg( my_window->UserPort );
  259.  
  260.     /* Have we collected the message sucessfully? */
  261.     if(my_message)
  262.     {
  263.       /* After we have collected the message we can read it, and save any */
  264.       /* important values which we maybe want to check later: */
  265.       class = my_message->Class;      /* Save the IDCMP flag. */
  266.       address = my_message->IAddress; /* Save the address. */
  267.   
  268.       /* After we have read it we reply as fast as possible: */
  269.       /* REMEMBER! Do never try to read a message after you have replied! */
  270.       /* Some other process has maybe changed it. */
  271.       ReplyMsg( my_message );
  272.   
  273.       /* Check which IDCMP flag was sent: */
  274.       switch( class )
  275.       {
  276.         case CLOSEWINDOW:  /* The user selected the Close window gadget! */
  277.                close_me=TRUE;
  278.                break;
  279.                
  280.         case GADGETDOWN:   /* The user has pressed on one of the Boolean */
  281.                            /* gadgets. We have now to check which: */
  282.                if( address == (APTR) &my_first_gadget)
  283.                  printf("Gadget 1 Down\n");
  284.                else
  285.                  printf("Gadget 2 Down\n");
  286.   
  287.                /* We need to do some "casting" here again since APTR is a */
  288.                /* normal memory pointer, while &my_first_gadget is a */
  289.                /* pointer to a Gadget structure. It is actually the same */
  290.                /* thing but we need to explain this for the compiler. */
  291.   
  292.                break;
  293.                
  294.         case GADGETUP:     /* The user has released one of the Boolean */
  295.                            /* gadgets. We have now to check which: */
  296.                if( address == (APTR) &my_first_gadget)
  297.                  printf("Gadget 1 Up\n");
  298.                else
  299.                  printf("Gadget 2 Up\n");
  300.                break;
  301.       }
  302.     }
  303.   }
  304.  
  305.  
  306.  
  307.   /* We should always close the windows we have opened before we leave: */
  308.   CloseWindow( my_window );
  309.  
  310.  
  311.  
  312.   /* Close the Intuition Library since we have opened it: */
  313.   CloseLibrary( IntuitionBase );
  314.   
  315.   /* THE END */
  316. }
  317.  
  318.